// --------------通用模块-----------------------------------------------------------------------
// const { ethers } = require("hardhat");
const { ethers } = require("ethers");
require("dotenv").config();
const alchemyApiKey =  process.env.alchemyApiKey;
// console.log(alchemyApiKey+' here is key');
let provider=new ethers.AlchemyProvider("arbitrum",alchemyApiKey);
const addressOfBpet="0x6daf586b7370b14163171544fca24abcc0862ac5";
const addressOfMine="0x9862cb355C67Cbb692821e8e8Da7DceA2e1Fc6E7";
const transactionHash="0x533b293f6d8f76b16ebf26a1068fafdf793532fe8a39c63d46ba9de009020bc2";
const functionMap = {
    getBalance,//success
    getCode,//fail
    getStorageAtTwo,//success
    getTrancationCount,//success
    getBlock,//success
    getBlockWithTransactions,//fail
    getResolver,//wait
    lookupAddress,//wait
    EnsResolver,//wait
    getNetwork,//fail
    getBlockNumber,//success
    getGasPrice,//fail
    getFeeData,//success
    call,//wait
    estimateGas,//success
    getTransAction,//success
    getTransactionReceipt,//success
    sendTranaction,//fail
  };

const functionName = process.argv[2]; // 获取第三个参数，即要执行的函数名称

if (functionName in functionMap) {
  const func = functionMap[functionName];
  func(); // 调用对应的函数
} else {
  console.log('Unknown function');
} 
// -----------------------------------账户-----------------------------------------------

async function getBalance(){
    console.log(await provider.getBalance(addressOfMine));
}

function getCode(){
    provider.getCode(addressOfBpet).then((rs)=>{
        const code = rs.slice(2); // 去掉十六进制前缀 '0x'
        const readableCode = Buffer.from(code, 'hex').toString();
        console.log(readableCode);//输出结果存在乱码
     });   
}
function getStorageAtTwo(){
    provider.getStorage(addressOfBpet,2).then((rs)=>{//todo 二号插槽存在数据,所以插槽的规则是怎么放的
        console.log(rs);
     });       
}    
function getTrancationCount(){
    provider.getTransactionCount(addressOfMine).then((rs)=>{
        console.log(rs);//47
    })
}
// ----------------------------------------区块------------------------------------------------
async function  getBlock (){
    const currentBlockNum=await provider.getBlockNumber();
    console.log(await provider.getBlock(currentBlockNum));//success
}
async function getBlockWithTransactions(){
   const currentBlockNum=await provider.getBlockNumber();
   console.log(await provider.getBlockWithTransactions(currentBlockNum));//notFunction
}
//------------------------------------ens------------------------------
async function getResolver(){
    const resolver = await provider.getResolver("ricmoo.eth");//revert
    // var name= await provider.resolveName("ricmoo.eth");
    // console.log(name);
}
async function lookupAddress(){
   console.log(await provider.lookupAddress("0x5555763613a12D8F3e73be831DFf8598089d3dCa"));//null
}
async function EnsResolver(){
    
// (这个会 match provider.resolveName)
var resolver=await provider.getResolver("example.arbitrum");//Cannot read properties of undefined (reading 'getAddress')
// '0x5555763613a12D8F3e73be831DFf8598089d3dCa'
console.log(resolver.getAddress());
// 为其他coin地址指定coinType (0 = Bitcoin)
await provider.resolver.getAddress(0);
// '1RicMooMWxqKczuRCa5D2dnJaUEn9ZJyn'
}
// ------------------网络----------------------------------
async function getNetwork(){
    await console.log(await provider.getNetwork());//Network {}
}
async function getBlockNumber(){
   var currentBlockNum=await provider.getBlockNumber();
   console.log(currentBlockNum);//181956231

}
async function getGasPrice(){
    gasPrice = await provider.getGasPrice();// not a function
    console.log(gasPrice);
    // ethers.utils.formatUnits(gasPrice, "gwei")
} 
async function getFeeData(){
    feeData = await provider.getFeeData();//Cannot read properties of undefined
    console.log(feeData);//success
    // ethers.utils.formatUnits(feeData.maxFeePerGas, "gwei")

}
// --------------------------------交易方法-----------------------------
async function call(){
    await console.log(await provider.call({
        // ENS public resovler address
        to: "0x4976fb03C32e5B8cfe2b6cCB31c09Ba78EBaBa41",
      
        // `function addr(namehash("ricmoo.eth")) view returns (address)`
        data: "0x3b3b57debf074faa138b72c65adbdcfb329847e4f2c04bde7f7dd7fcad5a52d2f395a558"
      }));//0x

}
async function estimateGas(){
    console.log(await provider.estimateGas({
        // Wrapped ETH address
        to: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
      
        // `function deposit() payable`
        data: "0xd0e30db0",
      
        // 1 ether
        value: ethers.parseEther("1.0")
      }));//448045n
}

async function getTransAction(){

    console.log(await provider.getTransaction(transactionHash));//success
}
async function getTransactionReceipt(){
    console.log(await provider.getTransactionReceipt(transactionHash));//success
}
async function sendTranaction(){
    const signedTx = "0x02f874827a69048459682f00845e558eb4825209945555763613a12d8f3e73be831dff8598089d3dca882b992b75cbeb600080c080a0c8a8f1bed41823367b739625e724e8ff89bd0caff38e5747cf76384c3ae3e26da03893b6aaefa8337cd67fbd4ceaf4aec44dfc928d78c040118a6cc8f9ea94367e";
console.log(await provider.sendTranaction(signedTx));//not a function
}
